package com.example.imitation_webchat_demo.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.imitation_webchat_demo.R;
import com.example.imitation_webchat_demo.adapter.SettingAdapter;
import com.example.imitation_webchat_demo.model.Resource;
import com.example.imitation_webchat_demo.util.ApplicationUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

//设置页面
public class SettingActivity extends Activity {
	@ViewInject(R.id.setting_head_back)
	private ImageView backView;
	
	@ViewInject(R.id.setting_listview)
	private ListView listView;
	
	private List<String> list;
	private SettingAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_setting);
		
		ViewUtils.inject(this);
		
		initComponent();      //初始化控件
		addListener();         // 添加监听
		
		ApplicationUtils.getInstance().addActivity(this);
	}
	
	//初始化控件
	private void initComponent(){
		list = new ArrayList<String>();
		list.add("新消息提醒");
		list.add("关于微信");
		list.add("退出当前账号");
		list.add("关闭微信");
		adapter = new SettingAdapter(this, list);
		listView.setAdapter(adapter);
	}
	
	//添加监听
	private void addListener(){
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch(position){
				case 0:
					break;
				case 1:
					break;
				case 2:
					putUsernameIntoShare();
					Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
					SettingActivity.this.startActivity(intent);
					ApplicationUtils.getInstance().removeActivity("MainActivity");
					finish();
					break;
				case 3:
					ApplicationUtils.getInstance().exit();
					break;
				}
			}
		});
	}
	
	//将sharepreference中的username变量值置空
	private void putUsernameIntoShare(){
		Editor editor = getSharedPreferences(Resource.appName, Context.MODE_PRIVATE).edit();
		editor.putString("username", null);
		editor.commit();
	}
	
	//点击返回图片的响应
	@OnClick(R.id.setting_head_back)
	private void back(View view){
		finish();
	}

	@Override
	protected void onDestroy() {
		ApplicationUtils.getInstance().removeActivity(this);
		super.onDestroy();
	}
}